package com.ruoyi.procurementmanagement.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.procurementmanagement.domain.ErpWarehouseEntry;
import com.ruoyi.procurementmanagement.service.IErpWarehouseEntryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procurementmanagement.domain.ErpSupplierSettlement;
import com.ruoyi.procurementmanagement.service.IErpSupplierSettlementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商结算单Controller
 *
 * @author ruoyi
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/procurementmanagement/settlement")
public class ErpSupplierSettlementController extends BaseController
{
    @Autowired
    private IErpSupplierSettlementService erpSupplierSettlementService;

    @Autowired
    private IErpWarehouseEntryService erpWarehouseEntryService;

    /**
     * 查询供应商结算单列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpSupplierSettlement erpSupplierSettlement)
    {
        startPage();
        List<ErpSupplierSettlement> list = erpSupplierSettlementService.selectErpSupplierSettlementList(erpSupplierSettlement);
        return getDataTable(list);
    }

    /**
     * 导出供应商结算单列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:export')")
    @Log(title = "供应商结算单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSupplierSettlement erpSupplierSettlement)
    {
        List<ErpSupplierSettlement> list = erpSupplierSettlementService.selectErpSupplierSettlementList(erpSupplierSettlement);
        ExcelUtil<ErpSupplierSettlement> util = new ExcelUtil<ErpSupplierSettlement>(ErpSupplierSettlement.class);
        util.exportExcel(response, list, "供应商结算单数据");
    }

    /**
     * 获取供应商结算单详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(erpSupplierSettlementService.selectErpSupplierSettlementById(id));
    }

    /**
     * 新增供应商结算单
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:add')")
    @Log(title = "供应商结算单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpSupplierSettlement erpSupplierSettlement)
    {
        return toAjax(erpSupplierSettlementService.insertErpSupplierSettlement(erpSupplierSettlement));
    }

    /**
     * 修改供应商结算单
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:edit')")
    @Log(title = "供应商结算单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpSupplierSettlement erpSupplierSettlement)
    {
        return toAjax(erpSupplierSettlementService.updateErpSupplierSettlement(erpSupplierSettlement));
    }

    /**
     * 删除供应商结算单
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:remove')")
    @Log(title = "供应商结算单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(erpSupplierSettlementService.deleteErpSupplierSettlementByIds(ids));
    }

    // ErpSupplierSettlementController.java
    /**
     * 取消并删除供应商结算单
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:settlement:cancel')")
    @Log(title = "供应商结算单", businessType = BusinessType.DELETE)
    @PostMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id) {
        try {
            int result = erpSupplierSettlementService.cancelErpSupplierSettlement(id);
            return toAjax(result);
        } catch (Exception e) {
            return error("取消结算单失败: " + e.getMessage());
        }
    }
}
